package com.hsu.hiring.talent.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hsu.hiring.talent.dao.TalentDao;
import com.hsu.hiring.talent.entity.TalentEntity;

/**
 * Talent Servlet
 *
 * @author Hsu.
 * @since 2016年1月11日
 */
public class TalentServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	private TalentDao talentDao = new TalentDao();

	/**
	 * 表单的method="get"或地址栏提交会调用到此方法
	 */
	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	/**
	 * 表单的method="post"提交会调用到此方法
	 */
	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// 避免中文乱码
		request.setCharacterEncoding("UTF-8");
		String command = request.getParameter("command");
		if ("register".equals(command)) {
			register(request, response);
		} else if ("login".equals(command)) {
			login(request, response);
		} else if ("get".equals(command)) {
			get(request, response);
		} else {
			throw new IllegalStateException("请求非法~");
		}
	}

	/**
	 * 求职者的注册
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void register(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// 1、获取前台传过来的参数
		String code = request.getParameter("code");
		String password = request.getParameter("password");
		String name = request.getParameter("name");
		String phone = request.getParameter("phone");
		String email = request.getParameter("email");
		int age = Integer.parseInt(request.getParameter("age"));
		int gender = Integer.parseInt(request.getParameter("gender"));
		StringBuilder sb = new StringBuilder();
		String[] hobbies = request.getParameterValues("hobby");
		for (int i = 0; i < hobbies.length; i++) {
			if (i == hobbies.length - 1) {
				sb.append(hobbies[i]);
			} else {
				sb.append(hobbies[i] + "|");
			}
		}

		// 2、调用业务逻辑
		TalentEntity talent = new TalentEntity();
		talent.setCode(code);
		talent.setPassword(password);
		talent.setName(name);
		talent.setPhone(phone);
		talent.setEmail(email);
		talent.setAge(age);
		talent.setGender(gender);
		talent.setHobby(sb.toString());
		talentDao.resiter(talent);

		// 3、转向
		request.getRequestDispatcher("view/talent/talent-login.jsp").forward(
				request, response);
	}

	/**
	 * 求职者登录
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void login(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 1、获取前台传过来的参数
		String code = request.getParameter("code");
		String password = request.getParameter("password");

		// 2、调用业务逻辑
		TalentEntity talent = talentDao.login(code, password);
		// 3、转向
		if (talent == null) {
			request.setAttribute("code", code);
			request.setAttribute("errorInfo", "用户名或密码错误！");
			request.getRequestDispatcher("view/talent/talent-login.jsp")
					.forward(request, response);
		} else {
			request.setAttribute("talent", talent);
			request.getRequestDispatcher("view/talent/main.jsp").forward(
					request, response);
		}
	}

	/**
	 * 求职者个人资料查询
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void get(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 1、获取前台传过来的参数
		int id = Integer.parseInt(request.getParameter("id"));

		// 2、调用业务逻辑
		TalentEntity talent = talentDao.get(id);
		// 3、转向
		request.setAttribute("talent", talent);
		request.getRequestDispatcher("view/talent/talent-info.jsp").forward(
				request, response);
	}
}
